<template>
	<view class="content">
		<my-bar>
			<block slot="content">聊段子</block>
			<block slot="right">
				<button class="cu-btn bg-gradual-green margin-right-xs">发布</button>
			</block>
		</my-bar>
		<form>
			<!-- !!!!! placeholder 在ios表现有偏移 建议使用 第一种样式 -->
			<view class="cu-form-group">
				<textarea maxlength="-1" adjust-position="false" @input="textareaInput" placeholder="聊聊有趣的事,让课堂更精彩..."></textarea>
			</view>
			<view class="cu-bar bg-white text-grey shadow shadow-lg">
				<view class="action text-xs">
					{{textareaValue.length}}/{{publish.contentTextCount}}
				</view>
				<view class="action">
				</view>
			</view>
			<view class="cu-form-group img-list">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
						<image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red text-df" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids " @tap="ChooseImage" v-if="imgList.length<8">
						<text class='cuIcon-cameraadd'>
							<text class="text-sm">
								{{imgList.length}}/{{publish.uploadImgCount}}
							</text>
						</text>
					</view>
				</view>
			</view>
			<view class="cu-form-group tag-title">
				<text class="text-gree">#添加标签</text>
			</view>
			<view class="cu-form-group padding-sm flex flex-wrap justify-around">
				<view class='cu-tag radius padding margin-xs' :class="'bg-'+randomColor(index)" v-for="(item,index) in tags.list"
				 :key="index">{{item.text}}</view>
			</view>
			<app-modal ref='modal' content="主人真的要无情的抛弃我吗？" :buttons="['再想想','残忍说再见']"></app-modal>
		</form>
	</view>
</template>

<script>
	import appModal from '@/components/my-modal.vue'
	import {
		mapState
	} from 'vuex'
	export default {
		components: {
			appModal
		},
		data() {
			return {
				imgList: [],
				textareaValue: ''
			};
		},
		methods: {
			textareaInput(e) {
				this.textareaValue = e.detail.value
			},
			ChooseImage() {
				uni.chooseImage({
					count: this.imgCount, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				var that = this;
				this.$refs.modal.open(function() {
					that.imgList.splice(e.currentTarget.dataset.index, 1)
				});
				/* uni.showModal({
					title: '召唤师',
					content: '确定要删除精彩瞬间吗？',
					cancelText: '再想想',
					confirmText: '残忍再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				}) */
			}
		},
		computed: {
			...mapState("base", ['tags', 'publish', 'colors'])
		},
		methods: {
			randomColor(idx) {
				if (idx >= this.colors.length) {
					idx = idx % this.colors.length;
				}
				return this.colors[idx];
			}
		}
	}
</script>

<style>
	page {
		background: #FFFFFF;
	}

	.cu-form-group {
		border: none;
	}

	.cu-form-group+.cu-form-group {
		border-top: 0;
	}

	.cu-form-group.tag-title {
		margin-top: 35rpx;
		border-top: 1px solid #eee;
	}

	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.cu-bar .action.text-xs {
		font-size: 12px;
	}

	.img-list {
		padding-top: 14px;
		;
	}
</style>
